﻿using Autofac.Core;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace XTemplate.Core.Swagger
{
    public static class SwaggerServiceExtensions
    {
        public static IServiceCollection AddCoreSwagger(this IServiceCollection services)
        {
            services
                .AddSwaggerGen(option =>
                {
                    // 添加请求头验证
                    option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description = "Json Web Token 验证请求头使用Bearer模式. 请求头参数示例: \"Authorization: Bearer {token}\"",
                        Name = "Authorization",//Jwt default param name
                        In = ParameterLocation.Header,//Jwt store address
                        Type = SecuritySchemeType.ApiKey//Security scheme type
                    });

                    // 加验证类型为Bearer
                    option.AddSecurityRequirement(new OpenApiSecurityRequirement {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference
                                {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                }
                            },
                            Array.Empty<string>()
                        }
                    });

                    option.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = "Template.Api",
                        Description = "spd DepartmentApp ASP.NET Core Web API",
                    });

                    // IGeekFan.AspNetCore.Knife4jUI 必须的配置
                    option.AddServer(new OpenApiServer()
                    {
                        Url = "",
                        Description = "vvv"
                    });
                    // IGeekFan.AspNetCore.Knife4jUI 必须的配置
                    option.CustomOperationIds(apiDesc =>
                    {
                        var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
                        return controllerAction.ControllerName + "-" + controllerAction.ActionName;
                    });

                    // Set the comments path for the Swagger JSON and UI.
                    var assName = Assembly.GetExecutingAssembly().GetName().Name;
                    var files = System.IO.Directory.GetFiles(AppContext.BaseDirectory, "*.xml").Where(x =>
                    {
                        var file = new FileInfo(x);
                        return file.Name.StartsWith(assName.Substring(0, assName.LastIndexOf('.')));
                    });
                    foreach (var item in files)
                    {
                        option.IncludeXmlComments(item);
                    }
                });

            return services;
        }
    }
}
